import 'dart:typed_data';

import 'package:bookshelf/util/convert_util.dart';
import 'package:flutter/services.dart';

//询查标签
Future inventoryWithAnt(List<String> ants) async {
  var array = [0x1B];
  var contentByte = '0001060901';
  contentByte += ants.length.toRadixString(16).padLeft(2,'0');
  for (var element in ants) {
    if(element.length == 4){
      var str1 = element.substring(0,2);
      var str2 = element.substring(2,4);
      contentByte += str1;
      contentByte += str2;
    }
  }
  contentByte += '000000';
  contentByte = '00${((contentByte.length)~/2).toRadixString(16).padLeft(2,'0')}$contentByte';
  var crc = await const MethodChannel('toAndroid').invokeMethod('crc',{'message':contentByte});
  contentByte += crc;
  int i = 0;
  while(i<contentByte.length){
    array.add(int.parse("0x${contentByte.substring(i,i+2).padLeft(2,'0')}"));
    i = i+2;
  }
  Uint8List bytes = Uint8List.fromList(array);
  print(bytes);
  return bytes;
}

//空包
empty(){
  var array = [0x1B,0x00,0x0B,0x00,0x01,0x06,0x09,0x01,0x01,0x02,0x01,0x00,0x00,0x00,0xC7,0x0B];
  Uint8List bytes = Uint8List.fromList(array);
  print(bytes);
  return bytes;
}

//读块
Future readBlocksWithAnt(String uid,String ant) async {

  var array = [0x1B];
  var contentByte = '0001060701${ant}000301${flip(uid)}01';
  contentByte = '00${((contentByte.length)~/2).toRadixString(16).padLeft(2,'0')}$contentByte';
  var crc = await const MethodChannel('toAndroid').invokeMethod('crc',{'message':contentByte});
  contentByte += crc;
  int i = 0;
  while(i<contentByte.length){
    array.add(int.parse("0x${contentByte.substring(i,i+2).padLeft(2,'0')}"));
    i = i+2;
  }
  Uint8List bytes = Uint8List.fromList(array);
  print(bytes);
  return bytes;
}


Future openLight(String ant) async {
  var array = [0x1B];
  var contentByte = '000106050101${ant}200A';
  contentByte = '00${((contentByte.length)~/2).toRadixString(16).padLeft(2,'0')}$contentByte';
  var crc = await const MethodChannel('toAndroid').invokeMethod('crc',{'message':contentByte});
  contentByte += crc;
  int i = 0;
  while(i<contentByte.length){
    array.add(int.parse("0x${contentByte.substring(i,i+2).padLeft(2,'0')}"));
    i = i+2;
  }
  Uint8List bytes = Uint8List.fromList(array);
  print(bytes);
  return bytes;
}

Future closeLight(String ant) async {
  var array = [0x1B];
  var contentByte = '000106050101${ant}0000';
  contentByte = '00${((contentByte.length)~/2).toRadixString(16).padLeft(2,'0')}$contentByte';
  var crc = await const MethodChannel('toAndroid').invokeMethod('crc',{'message':contentByte});
  contentByte += crc;
  int i = 0;
  while(i<contentByte.length){
    array.add(int.parse("0x${contentByte.substring(i,i+2).padLeft(2,'0')}"));
    i = i+2;
  }
  Uint8List bytes = Uint8List.fromList(array);
  print(bytes);
  return bytes;
}

// Future closeLight() async {
//   var array = [0x1B];
//   var contentByte = '00010605010102010020';
//   contentByte = '00${((contentByte.length)~/2).toRadixString(16).padLeft(2,'0')}$contentByte';
//   var crc = await const MethodChannel('toAndroid').invokeMethod('crc',{'message':contentByte});
//   contentByte += crc;
//   int i = 0;
//   while(i<contentByte.length){
//     array.add(int.parse("0x${contentByte.substring(i,i+2).padLeft(2,'0')}"));
//     i = i+2;
//   }
//   Uint8List bytes = Uint8List.fromList(array);
//   print(bytes);
//   return bytes;
// }

